| Conditions | 2 |
| Paths | 2 |
| Total Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | const {MongoClient} = require('mongodb');
|
||
| 3 | MongoClient.connect('mongodb://localhost:27017/JokesApp', (err, db) => {
|
||
| 4 | if (err){
|
||
| 5 | return console.log('failed to connect to mongodb server. ', err);
|
||
|
|
|||
| 6 | } |
||
| 7 | |||
| 8 | console.log('Connected to MongoDB Server');
|
||
| 9 | |||
| 10 | //deleteMany - delete all the occcurences |
||
| 11 | db.collection('Jokes').deleteMany({joke : 'Sample joke 2'}).then((result) => {
|
||
| 12 | console.log(result.result); |
||
| 13 | }, (err) => {
|
||
| 14 | cosole.log('Error deleting many. E : ', err)
|
||
| 15 | }); |
||
| 16 | |||
| 17 | //deleteOne - deletes only first occurence of all the occurences |
||
| 18 | db.collection('Jokes').deleteOne({joke : 'Test 1'}).then((result) => {
|
||
| 19 | console.log(result.result); |
||
| 20 | }); |
||
| 21 | |||
| 22 | db.collection('Jokes').findOneAndDelete({joke : 'Test 2'}).then((doc) => {
|
||
| 23 | console.log(doc); |
||
| 24 | }, (err) => {
|
||
| 25 | console.log('Error find one and delete. E: ', err);
|
||
| 26 | }); |
||
| 27 | |||
| 28 | //close the DB |
||
| 29 | // db.close(); |
||
| 30 | }); |